import wepy from 'wepy'
export default class extends wepy.mixin {
    data = {
        goods_id: 0,
        goodsDetailInfo: [],
        address: null
    }

    methods = {
        // 图片预览
        imgPreview(img) {
            wepy.previewImage({
                current: img,
                urls: this.goodsDetailInfo.pics.map(x => x.pics_big)
            })
        },

        // 获取地址
        async getAddress() {
            var res = await wepy.chooseAddress().catch(err => err)
            console.log(res);

            if (res.errMsg != 'chooseAddress:ok') {
                return wepy.baseToast('获取收货地址失败')
            }
            this.address = res
            wepy.setStorageSync('address', res)
            this.$apply()
        },

        // 添加商品到购物车列表
        addToCar() {
            console.log(123);

            // console.log(this.goodsDetailInfo);
            this.$parent.addGoodsToCar(this.goodsDetailInfo)
            wepy.showToast({
                title: '已添加到购物车',
                icon: 'success'
            })

        }

    }

    computed = {
        addressStr() {
            if (this.address == null) {
                return '请选择收货地址'
            }
            return this.address.provinceName +
                this.address.cityName +
                this.address.countyName
        }
    }

    onLoad(o) {
        console.log(o)
        this.goods_id = o.goods_id
        this.getGoodsDetilInfo()
    }

    async getGoodsDetilInfo(goods_id) {

        var { data: res } = await wepy.get('/goods/detail?goods_id=' + this.goods_id)
        console.log(res)

        if (res.meta.status != 200) {
            return wepy.baseToast()
        }
        this.goodsDetailInfo = res.message
        this.$apply()
    }




}